import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import autoprefixer from "autoprefixer";
import postCssPxToRem from "postcss-pxtorem";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const config = loadEnv(mode, __dirname)
  return {
    plugins: [
      vue(),
      vueJsx(),
    ],
    base: config.VITE_APP__STATIC_PATH,
    build: {
      minify: "terser",
      sourcemap: false, // 不生成 source map
      terserOptions: {
        compress: { // 打包时清除 console 和 debug 相关代码
          drop_console: false,
          drop_debugger: false,
        },
      },
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return 'vendor';
            }
          },
        },
      },
      chunkSizeWarningLimit: 20000,
    },
    server: {
      port: 8100,
      host: '0.0.0.0',
      proxy: {
        '/web-sso': {
          target: 'http://192.168.129.44:8000/web-sso',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/web-sso/, '')
        },
        '/authorization': {
          target: config.VITE_APP__REQUEST_URL + '/authorization',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/authorization/, '')
        },
        '/api': {
          target: 'http://ing-ms.tsc-microservice.192.168.129.160.nip.io',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },
        '/iat-service': {
          ws: false,
          target: config.VITE_APP__REQUEST_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/iat-service/, '')
        },
        // '/service': {
        //   ws: false,
        //   target: config.VITE_APP__REQUEST_URL,
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/service/, '')
        // },
      }
    },
    publicPath: process.env.VITE_APP__PUBLIC_PATH,
    // css: {
    //   postcss: {
    //     plugins: [
    //       autoprefixer({
    //         overrideBrowserslist: [
    //           "Android 4.1",
    //           "iOS 7.1",
    //           "Chrome > 31",
    //           "ff > 31",
    //           "ie >= 8",
    //         ],
    //       }),
    //       postCssPxToRem({
    //         // 自适应，px>rem转换
    //         rootValue: 190, // 75表示750设计稿，37.5表示375设计稿
    //         propList: ["*"], // 需要转换的属性，这里选择全部都进行转换 也可以'font', 'font-size', 'line-height', 'letter-spacing'等
    //         // // selectorBlackList: [".van"], // 过滤掉van-开头的class，不进行rem转换
    //         // exclude: "/node_modules", // 忽略包文件转换rem
    //       }),
    //     ],
    //   },
    // },
    //解决icon图片偶尔出现乱码
    esbuild: {
      charset: 'ascii'
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
  }
})
